package main

import (
	"context"
	"fmt"
	"log"
	"sort"
	"sync"
	"time"
)

// 计算 成交量
func (this *Trade) CronCalcTrading(wg *sync.WaitGroup, ctx context.Context, symbol string) {
	first := true
	oldGap := struct {
		LastUpdate int64
		Gap        float64
	}{}
	defer wg.Done()
	t := time.NewTicker(10 * time.Second)
	defer t.Stop()
	for {
		select {
		case <-ctx.Done():
			log.Println("退出成交量")
			return
		case <-t.C:
			size := "200"
			if first {
				size = "2000"
			}
			// 交易数据
			coinTrade, err := this.client.GetCoinTrade(symbol, size)
			if err != nil {
				log.Println("请求出错", err)
				continue
			}
			first = false
			if len(coinTrade.Data) < 1 {
				continue
			}
			if this.MACD.List == nil {
				this.MACD.List = map[int64]struct {
					BuyAmount  float64
					SellAmount float64
					Gap        float64
				}{}
			}
			for _, l := range coinTrade.Data {
				for _, v := range l.Data {
					if v.TS <= this.MACD.LastUpdate {
						continue
					}
					ct := v.TS / 1000
					k := ct - ct%900
					if _, ok := this.MACD.List[k]; !ok {
						this.MACD.List[k] = struct {
							BuyAmount  float64
							SellAmount float64
							Gap        float64
						}{BuyAmount: 0, SellAmount: 0, Gap: 0}
					}
					macd := this.MACD.List[k]
					if v.Direction == "buy" {
						macd.BuyAmount += v.Quantity
					}
					if v.Direction == "sell" {
						macd.SellAmount += v.Quantity
					}
					macd.Gap = macd.BuyAmount - macd.SellAmount
					this.MACD.List[k] = macd
				}
			}

			this.MACD.LastUpdate = coinTrade.Data[0].Data[0].TS
			var keys []int
			for k := range this.MACD.List {
				keys = append(keys, int(k))
			}
			sort.Ints(keys)
			arr := []float64{}
			for i, k := range keys {
				t := int64(k)
				macd := this.MACD.List[t]
				log.Println(time.Unix(t, 0).Format("2006-01-02 15:04:05"),
					"当前15分钟买入量", fmt.Sprintf("%.2f", macd.BuyAmount),
					"当前15分钟卖出量", fmt.Sprintf("%.2f", macd.SellAmount),
					"当前15分钟 MACD:", fmt.Sprintf("%.2f", macd.BuyAmount-macd.SellAmount))
				if i > len(keys)-4 {
					arr = append(arr, macd.Gap)
				}
			}
			this.RecommendDirection = -1
			if this.forceBuy == 0 && this.forceSell == 0 { // 如果有单边 就进行单边策略
				if len(arr) >= 4 {
					if arr[0] > 0 && arr[1] >= arr[0] && arr[2] >= arr[1] {
						this.RecommendDirection = 0 // 推荐买入
					}
					if arr[0] < 0 && arr[1] <= arr[0] && arr[2] <= arr[1] {
						this.RecommendDirection = 1 // 推荐做空
					}
				} else if len(arr) > 0 {
					if oldGap.LastUpdate > 0 {
						if this.MACD.LastUpdate/1000 > oldGap.LastUpdate/1000+60 {
							if arr[len(arr)-1] > oldGap.Gap {
								// 说明买单在增加
								this.RecommendDirection = 0 // 推荐买入
							} else {
								// 说明卖单在增加
								this.RecommendDirection = 1 // 推荐做空
							}
							oldGap.Gap = arr[len(arr)-1]
							oldGap.LastUpdate = this.MACD.LastUpdate
						}
					} else {
						oldGap.Gap = arr[len(arr)-1]
						oldGap.LastUpdate = this.MACD.LastUpdate
					}
				}
			}

			if len(keys) > 16 { // 只留最近 16个 15分钟 交易数据  即 4小时数据 做参考
				// 删除最早的一个
				delete(this.MACD.List, int64(keys[0]))
			}
		}
	}
}
